package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConFood;
import com.htgd.gardenexpo.entity.GeConIntegralCategory;
import com.htgd.gardenexpo.page.FoodQuery;
import com.htgd.gardenexpo.service.GeConIntegralCategoryService;
import com.htgd.gardenexpo.vo.IntegralCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分类目 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-31
 */
@RestController
@RequestMapping("/api/intergralCategory")
@Api(tags = "积分类目")
public class GeConIntegralCategoryController {

    @Autowired
    private GeConIntegralCategoryService geConIntegralCategoryService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConIntegralCategory geConIntegralCategory) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralCategory.setStatus("1");
            geConIntegralCategoryService.save(geConIntegralCategory);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeConIntegralCategory geConIntegralCategory) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralCategoryService.updateById(geConIntegralCategory);
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete(int id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralCategoryService.delete(id);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConIntegralCategory>> list(GeConIntegralCategory geConIntegralCategory) {
        CommonResult<List<GeConIntegralCategory>> result = new CommonResult<>();
        try {
            IntegralCategoryVo list = geConIntegralCategoryService.querylist(geConIntegralCategory);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }
}
